package com.wlf.demo.pageHelper.api;

import com.wlf.demo.pageHelper.model.Pagination;
import com.wlf.demo.pageHelper.model.dto.UserDTO;
import com.wlf.demo.pageHelper.model.vo.UserVO;
import com.wlf.demo.pageHelper.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserApi {

    @Autowired
    private UserService userService;

    @GetMapping("list")
    public Pagination<UserVO> list(
            @Valid UserDTO userDTO,
            @PageableDefault(page=1,size=6)
            @SortDefault.SortDefaults({
                    @SortDefault(sort = "user_id", direction = Sort.Direction.ASC)
            }) Pageable pageable
    ) {

        return userService.findByPage(userDTO, pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort().toString().replaceAll(":", ""));

    }

}
